<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Vote
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 * @version    $Id: Create.php 2011-03-09 08:45:41Z idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Vote
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 */

class Vote_Form_Create extends Engine_Form {
	public function init() {
		parent::init ();
		
		$this->setTitle ( 'VOTING_CREATE_TITLE' )->setDescription ( 'VOTING_CREATE_DESC' )->setMethod ( 'post' )->setAttrib ( 'class', 'global_form create_voting_form' );
		
		$user = Engine_Api::_ ()->user ()->getViewer ();
		$user_level = Engine_Api::_ ()->user ()->getViewer ()->level_id;
		
		$this->addElement ( 'Text', 'title', array ('label' => 'Title', 'allowEmpty' => false, 'required' => true, 'validators' => array (array ('NotEmpty', true ), array ('StringLength', false, array (1, 64 ) ) ), 'filters' => array ('StripTags', new Engine_Filter_Censor (), new Engine_Filter_EnableLinks () ) ) );
		
		$allowed_html = Engine_Api::_ ()->authorization ()->getPermission ( $user_level, 'vote_challenge', 'auth_html' );
		
		$this->addElement ( 'TinyMce', 'description', array ('disableLoadDefaultDecorators' => true, 'required' => true, 'allowEmpty' => false, 'label' => 'Description', 'decorators' => array ('ViewHelper' ), 'filters' => array (new Engine_Filter_Censor (), new Engine_Filter_Html ( array ('AllowedTags' => $allowed_html ) ) ) ) );
		
		$this->addElement ( 'Text', 'tags', array ('label' => 'Tags (Keywords)', 'autocomplete' => 'off', 'description' => 'Separate tags with commas.', 'filters' => array (new Engine_Filter_Censor () ) ) );
		
		$this->addElement ( 'Select', 'category_id', array ('label' => 'Category' ) );
		
		$this->addElement ( 'File', 'photo', array ('label' => 'Photo' ) );
		
		$start = new Engine_Form_Element_CalendarDateTime ( 'starttime' );
		$start->setLabel ( "Start Time" );
		$start->setAttrib ( 'required', true );
		$start->setAttrib ( 'allowEmpty', false );
		$this->addElement ( $start );
		
		// End time
		$end = new Engine_Form_Element_CalendarDateTime ( 'endtime' );
		$end->setLabel ( "End Time" );
		$end->setAttrib ( 'required', true );
		$end->setAttrib ( 'allowEmpty', false );
		$this->addElement ( $end );
		
		$this->tags->getDecorator ( "Description" )->setOption ( "placement", "append" );
		$this->photo->addValidator ( 'Extension', false, 'jpg,png,gif,bmp' );
		
		$this->addElement ( 'Checkbox', 'search', array ('label' => 'People can search for this voting', 'value' => True ) );
		
		$availableLabels = array ('everyone' => 'Everyone', 'registered' => 'All Registered Members', 'owner_network' => 'Friends and Networks', 'owner_member_member' => 'Friends of Friends', 'owner_member' => 'Friends Only', 'owner' => 'Just Me' );
		
		// Element: auth_view
		$viewOptions = ( array ) Engine_Api::_ ()->authorization ()->getAdapter ( 'levels' )->getAllowed ( 'vote_challenge', $user, 'auth_view' );
		$viewOptions = array_intersect_key ( $availableLabels, array_flip ( $viewOptions ) );
		
		if (count ( $viewOptions ) >= 1) {
			$this->addElement ( 'Select', 'auth_view', array ('label' => 'Privacy', 'description' => 'Who may see this voting?', 'multiOptions' => $viewOptions, 'value' => key ( $viewOptions ) ) );
			$this->auth_view->getDecorator ( 'Description' )->setOption ( 'placement', 'append' );
		}
		
		$availableLabels = array ('registered' => 'All Registered Members', 'owner_network' => 'Friends and Networks', 'owner_member_member' => 'Friends of Friends', 'owner_member' => 'Friends Only', 'owner' => 'Just Me' );
		
		$commentOptions = ( array ) Engine_Api::_ ()->authorization ()->getAdapter ( 'levels' )->getAllowed ( 'vote_challenge', $user, 'auth_comment' );
		$commentOptions = array_intersect_key ( $availableLabels, array_flip ( $commentOptions ) );
		
		if (count ( $commentOptions ) >= 1) {
			$this->addElement ( 'Select', 'auth_comment', array ('label' => 'Comment Privacy', 'description' => 'Who may post comments on this voting?', 'multiOptions' => $commentOptions, 'value' => key ( $commentOptions ) ) );
			$this->auth_comment->getDecorator ( 'Description' )->setOption ( 'placement', 'append' );
		}
		
		$partOptions = ( array ) Engine_Api::_ ()->authorization ()->getAdapter ( 'levels' )->getAllowed ( 'vote_challenge', $user, 'auth_participate' );
		$partOptions = array_intersect_key ( $availableLabels, array_flip ( $partOptions ) );
		
		if (count ( $partOptions ) >= 1) {
			$this->addElement ( 'Select', 'auth_participate', array ('label' => 'Participation Privacy', 'description' => 'Who may participate in this voting?', 'multiOptions' => $partOptions, 'value' => key ( $partOptions ) ) );
			$this->auth_participate->getDecorator ( 'Description' )->setOption ( 'placement', 'append' );
		}
		
		$voteOptions = ( array ) Engine_Api::_ ()->authorization ()->getAdapter ( 'levels' )->getAllowed ( 'vote_challenge', $user, 'auth_vote' );
		$voteOptions = array_intersect_key ( $availableLabels, array_flip ( $voteOptions ) );
		
		if (count ( $voteOptions ) >= 1) {
			$this->addElement ( 'Select', 'auth_vote', array ('label' => 'Vote Privacy', 'description' => 'Who may vote?', 'multiOptions' => $voteOptions, 'value' => key ( $voteOptions ) ) );
			$this->auth_vote->getDecorator ( 'Description' )->setOption ( 'placement', 'append' );
		}
		
		$this->addElement ( 'Button', 'submit', array ('label' => 'Create Voting', 'type' => 'submit', 'ignore' => true, 'decorators' => array ('ViewHelper' ) ) );
		
		$this->addElement ( 'Cancel', 'cancel', array ('label' => 'cancel', 'link' => true, 'prependText' => ' or ', 'decorators' => array ('ViewHelper' ) ) );
		
		$this->addDisplayGroup ( array ('submit', 'cancel' ), 'buttons', array ('decorators' => array ('FormElements', 'DivDivDivWrapper' ) ) );
	
	}
}